import { message } from "antd";
import axios from "axios";

const instance = axios.create({
    baseURL: "http://localhost:3001/",
    timeout: 3000,
});

instance.interceptors.request.use(
    (config) => {
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
)

instance.interceptors.response.use(
    (response) => {
        if(response.status === 200){
            return response.data;
        }else if (response.status === 401){
            
        }


        return response;
    },
    (error) => {
        console.log(222,error)
        if(error.response.status === 401){
            message.error("登录过期，请重新登录");
        }
        message.error(error.response.data.message);
        return Promise.reject(error);
    }
)

const get = (url: string,params?:object) => {
    return instance.get(url,{params});
}

const post = (url: string,data?:object) => {
    return instance.post(url,data);
}

const put = (url: string,data?:object) => {
    return instance.put(url,data);
}

const del = (url: string,id?:number) => {
    return instance.delete(url,{params:{id}});
}

export {
    get,
    post,
    put,
    del
}

